//评论表
const express=require('express')
const router=express.Router()
const pool=require('../utils/db')
const Response=require('../utils/Response')
//cyss_discuss
//查询所有
router.get('/discusslist', async (req,res)=>{
    let {page,pagesize} =req.query
    // console.log('评论列表', req.query);
    try {
        let startIndex=(page - 1) * pagesize
        let size = parseInt(pagesize)
        let sql="select * from cyss_discuss limit ?,?"
        let result = await pool.querySync(sql,[startIndex,size])
        let sql2 = "select count(*) as count from cyss_discuss"
        let result2 = await pool.querySync(sql2,[startIndex,size])
        let total = result2[0].count
        // console.log('result',result);
        console.log('result2',result2);
        res.send(Response.ok({page:parseInt(page),pagesize:size,total,result}))
    } catch(error) {
        res.send(Response.error(error))
    }
})

//用户ID 查询
router.post('/discuss-id',(req,res)=>{
    let {user_id}=req.body

    let sql = "select * from cyss_discuss where user_id = ?"
    pool.query(sql,[user_id],(error,result) => {
        if (error) {
            res.send(Response.error(500,error))
            throw error
        }
        res.send(Response.ok(result))
    })
})
// 线路页面查询相关线路评论的
router.post('/disLineID',(req,res) => {
    // console.log(req.body.id);

    let sql = "select * from cyss_discuss where line_id=?"
    pool.query(sql,[req.body.id],(error,result) => {
        if (error) {
            res.send(Response.error(500,error))
            throw error
        }
        res.send(Response.ok(result))
    })
})

//删除评论  
router.post('/discuss-del',(req,res)=>{
    let { id } = req.body
    // console.log('删除评论的dis_id', req.body);
    let sql="delete from cyss_discuss where dis_id=?"
    pool.query(sql,[id],(error,result)=>{
        if(error) {
            res.send(Response.error(500,error))
            throw error
        }
        res.send(Response.ok(result))
    })
})
//导出路由对象
module.exports=router